package com.pyk.service.impl;


import com.pyk.entity.User;
import com.pyk.mapper.UserMapper;
import com.pyk.service.AnswerService;
import com.pyk.service.UserService;
import com.pyk.utils.Md5Util;
import com.pyk.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
    User user=userMapper.findByUsername(username);
    return user;
    }

    @Override
    public void register(String username, String password) {
        //加密
        //MD5工具
        String md5string = Md5Util.getMD5String(password);
        userMapper.add(username,md5string);
        //添加
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id= (int)map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd, Integer userId) {
        String md5string = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(md5string,userId);
    }


    @Service
    public static class AnswerServicelmpl {
        @Service
        public class AnswerServiceImpl implements AnswerService {
            @Autowired
        private AnswerMapper answerMapper;

            @Override
            public void save(UserAnswer userAnswer) {
                userAnswer.getSubmitTime(LocalDateTime.now());
                answerMapper.insert(userAnswer);
            }




        }

    }
}
